# -*- coding: utf-8 -*-
# @Time : 2022/1/7 16:57
# @Author : Limusen
# @File : demo_base
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.implicitly_wait(10)
# driver.find_element(By.XPATH, '//*[@id="kw"]').send_keys("测试一下")
# time.sleep(2)
# driver.find_element(By.XPATH, '//*[@id="su"]').click()

# excel中取出来的数据格式
element = {'baidu_input': {'element_name': '百度输入框', 'locator_type': 'id', 'locator_value': 'kw', 'timeout': 5.0},
           'query_button': {'element_name': '点击查询按钮', 'locator_type': 'id', 'locator_value': 'su', 'timeout': 5.0}}

input_text = element['baidu_input']
query_button = element['query_button']


# 由于我们从字典中取出来的数据是这样
# 所以我们封装base_page 代码

def find_element(element_info):
    locator_type = element_info["locator_type"]
    locator_value = element_info["locator_value"]
    locator_timeout = element_info["timeout"]

    if locator_type == "name":
        locator_type = By.NAME
    elif locator_type == "css":
        locator_type = By.CSS_SELECTOR
    elif locator_type == "xpath":
        locator_type = By.XPATH
    elif locator_type == "id":
        locator_type = By.ID
    elif locator_type == "class":
        locator_type = By.CLASS_NAME
    elif locator_type == "linktext":
        locator_type = By.LINK_TEXT
    elif locator_type == "partiallink":
        locator_type = By.PARTIAL_LINK_TEXT
    elif locator_type == "tag":
        locator_type = By.TAG_NAME

    element = WebDriverWait(driver, locator_timeout).until(
        lambda x: x.find_element(locator_type, locator_value))

    return element


def click(element):
    element = find_element(element)

    element.click()


def send_keys(element, text):
    element = find_element(element)
    element.send_keys(text)


send_keys(element['baidu_input'],"测试一下")
click(element['query_button'])
